package niu.toy.spring.mvc.impl;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ClassUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class InternalResourceView extends AbstractView {
    private static final Logger logger = LoggerFactory.getLogger(InternalResourceView.class);

    private boolean alwaysInclude = false;

    private boolean preventDispatchLoop = false;

    public InternalResourceView() {
    }

    public InternalResourceView(String url) {
        this.setUrl(url);
    }

    /**
     * 使用model渲染视图
     */
    @Override
    protected void renderMergedOutputModel(
            Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        // Expose the model object as request attributes.
        exposeModelAsRequestAttributes(model, request);
        response.setContentType(this.getContentType());
        String classPath=ClassUtil.getClassPath();
        logger.info("classPath:"+classPath);
        String htmlFile=classPath+"\\templates\\"+getUrl();
        String html=FileUtil.readUtf8String(htmlFile);
        response.getWriter().write(html);
        response.flushBuffer();
    }

}
